package com.zhuzhe.blog.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuzhe.blog.aliyun.AliyunUtil;
import com.zhuzhe.blog.article.requestDto.AdvertSerachDto;
import com.zhuzhe.blog.article.responseVo.AdvertVo;
import com.zhuzhe.blog.base.JsonData;
import com.zhuzhe.blog.base.ResponseData;
import com.zhuzhe.blog.convert.Convert;
import com.zhuzhe.blog.article.entity.Advert;
import com.zhuzhe.blog.article.mapper.AdvertMapper;
import com.zhuzhe.blog.article.service.IAdvertService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuzhe.blog.properties.BlogProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 广告信息表 服务实现类
 * </p>
 *
 * @author u53
 * @since 2020-10-08
 */
@Service
public class AdvertServiceImpl extends ServiceImpl<AdvertMapper, Advert> implements IAdvertService {
    @Override
    public ResponseData<IPage<AdvertVo>> queryPage(AdvertSerachDto advertSerachDto) {
        QueryWrapper<Advert> wrapper = new QueryWrapper<>();

        if(StringUtils.isNotEmpty(advertSerachDto.getTitle())) {
            wrapper.like("title", advertSerachDto.getTitle());
        }

        if(advertSerachDto.getStatus() != null) {
            wrapper.eq("status", advertSerachDto.getStatus());
        }

        IPage<Advert> page = baseMapper.selectPage(advertSerachDto.getPage(), wrapper);

        //声明返回参数
        IPage<AdvertVo> advertListVoIPage = new Page<>();
        //数据集合
        List<AdvertVo> advertListVoList = new ArrayList<>();
        //值复制
        Convert.convertType(page,advertListVoIPage);
        for (Advert advert:page.getRecords()){
            AdvertVo advertVo = new AdvertVo();
            Convert.convertType(advert,advertVo);
            advertListVoList.add(advertVo);
        }
        advertListVoIPage.setRecords(advertListVoList);
        return JsonData.success(advertListVoIPage);
    }

    @Autowired
    private BlogProperties blogProperties;

    @Transactional
    @Override
    public ResponseData deleteById(String id) {
        // 1. 先通过广告id查询图片url
        Advert advert = baseMapper.selectById(id);
        if (null == advert){
            return JsonData.fail("改广告不存在");
        }
        String imageUrl = advert.getImageUrl();

        // 2. 先删除表中的广告信息
        baseMapper.deleteById(id);

        // 3. 删除oss上的图片
        if(StringUtils.isNotEmpty(imageUrl)) {
            AliyunUtil.delete(imageUrl, blogProperties.getAliyun());
        }
        return JsonData.success();
    }

    @Override
    public ResponseData<List<AdvertVo>> findByPosition(int position) {
        QueryWrapper<Advert> wrapper = new QueryWrapper<>();
        // 状态是正常的 status=1
        wrapper.eq("status", 1);
        // 位置
        wrapper.eq("position", position);
        // 排序 sort 升序
        wrapper.orderByAsc("sort");

        List<Advert> advertList =  baseMapper.selectList(wrapper);
        //声明返回参数
        List<AdvertVo> advertVoList = new ArrayList<>();
        for (Advert advert:advertList){
            AdvertVo advertVo = new AdvertVo();
            Convert.convertType(advert,advertVo);
            advertVoList.add(advertVo);
        }
        return JsonData.success(advertVoList);
    }

}
